@extends('admin.common.base-master')

@section('html-head')
    <script src="{{url('/vendor/My97DatePicker/WdatePicker.js')}}"></script>
    <link href="{{url('css/admin/pagenum.css')}}" rel="stylesheet" />
    <!-- 配置文件 -->
    <script type="text/javascript" src="{{url('vendor/ueditor/ueditor.config.js')}}"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="{{url('vendor/ueditor/ueditor.all.js')}}"></script>
    <style>
        .fill-list{
            line-height: 2.5;
        }
    </style>
    @endsection

    @section('html-body')
            <!-- content start -->
    <div id="content">
        <!-- cur-position start -->
        <div id="cur-position">
            <span class="tit">当前位置：</span>
            <a href="#">医生管理</a>
            <em>></em>
            <a href="#">添加医生信息</a>
        </div>
        <!-- cur-position end -->

        <div class="list-wrap">
            <h2 class="head">
                <span class="span1">医生休诊时间设置</span>
            </h2>

            <!-- list-text-box start -->
            <form id="addAdvertiserInfo">
                <div class="list-text-box">
                    <div class="fill-list">
                        <label class="fill-tit">医生名称：</label>
                        <div class="fill-cont">
                            某医生
                        </div>
                    </div>
                    <div class="fill-list">
                        <label class="fill-tit">选择时间：</label>
                        <div class="fill-cont">
                            <input class="fill-ipt" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd',minDate:'%y-%M-{%d+1}',onpicked:function(dp){changeTime(dp)}})" type="text" placeholder="请选择开始时间" name="stime" id="stime" value="{{$date}}" />
                        </div>
                    </div>
                    <div class="fill-list" id="date">
                        <label class="fill-tit">休诊时段：【周{{$week}}】</label>
                        <div class="fill-cont" id="time">
                            <input type="checkbox" name="checkall" id="allchecked" value="1">全选

                         @for($i=0;$i<6;$i++)
                            <input type="checkbox" name="date" value="{{$i+1}}" style="margin-left: 15px" @if((int)$hugs[$i] == 1)checked="checked" @endif>{{$times[$i]}}
                          @endfor

                        </div>
                    </div>
                    <input type="hidden" id="id" name="id" value="" />
                    <div class="fill-list">
                        <label class="fill-tit">&nbsp;</label>
                        <div class="fill-cont">
                            <input class="fill-submit" type="button" value="提交" />
                        </div>
                    </div>
                </div>
            </form>
            <!-- list-text-box end -->
        </div>
    </div>
    <!-- content end -->
    <script>

        jQuery(function($) {

            window.changeTime = function(dp) {
                // 选取时间
                var time = dp.cal.getDateStr();
                window.location.href="{{route('Admin.Index.doctorHug')}}?id={{$id}}&date="+time;
            }

            $("#allchecked").change(function(){
                var flag = $(this).is(':checked');
                $("[name ='date']").prop('checked',flag  ? true : false);
//                $("[name ='date']").attr('checked', flag  ? true : false);
            });
            $("[name ='date']").click(function(){
                var allcheckbox = $("[name ='date']");
                var flag = false;
                $.each(allcheckbox,function (index , item) {
                    if(!$(item).is(':checked')){
                        flag = true;
                    }
                });
                if(flag){
                    $("#allchecked").prop('checked' , false);
                }else{
                    $("#allchecked").prop('checked' , true);
                }
            });

            jQuery.ajaxSetup({
                case:false,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            function getTimeID(){
                var tmp = [];
                $("[name ='date']").each(function(index , object){
                    if($(object).is(':checked')){
                        tmp.push($(object).attr('value'));
                    }
                })
                return tmp;
            }
            var doc = document,
                    $doc = $(doc);

            $doc.on('click','.fill-submit',function(){
                // 医生ID
                var id = "{{$id}}";
                // 时间段ID
                var timeId = getTimeID();
                // 时间 日期
                var date = $('.fill-ipt').val();
                    $.ajax({
                        type:"post",
                        url:'{{route('Api.YiLiao.setDoctorHug')}}',
                        dataType:"json",
                        data:{
                            id : id,
                            hugs : timeId,
                            date : date,
                        },
                        success:function(obj){
                            if(obj.level == "success"){
                                alert(obj.message);
                                window.location.reload();
                            }
                        },
                        error:function(obj){
                            var datajson = obj.responseJSON;
                            alert(datajson.message);
                        }
                    });
            });

        });
    </script>
@endsection